
<script setup>
// import { User, Lock } from '@element-plus/icons-vue'
import { ref,watch } from 'vue'
import {UserLoginService,UserRegisterService} from '@/api/user'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
const userStore = useUserStore()
const router = useRouter()
const isRegister = ref(true) 
const ruleForm = ref({
  username: '',
  password: '',
  repassword: ''
})
const rules = {
  username: [
    { required: true, message: '用户名不能为空', trigger: 'change' },
    { min: 5, max: 15, message: '用户名必须是5-15个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
    {
      pattern: /^\S{6,25}$/,
      message: '密码必须是6-15个非空字符',
      trigger: 'blur'
    }
  ],
  repassword: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
    {
      pattern: /^\S{6,25}$/,
      message: '密码必须是6-15个非空字符',
      trigger: 'blur'
    },
    {
      validator: (rule, value, callback) => {
        if (value !== ruleForm.value.password) {
          callback(new Error('两次密码不一致'))
        } else {
          callback()
          // console.log();
        }
      },
      trigger: 'blur'
    }
  ]
}

const submit = async () => {
  await UserRegisterService(ruleForm.value)
  ElMessage.success('成功注册')
  isRegister.value = true
}

const login = async () => {
  const res = await UserLoginService(ruleForm.value)
  console.log(res);
  userStore.setToken(res.data.data)
  ElMessage.success('成功登录')
  console.log(res)
  router.push('./bill/layout')
}

watch(isRegister,()=>{
  ruleForm.value = {
    username: '',
    password: '',
    repassword: ''
  }
})
</script>

<template>
  <el-row class="login-page">
    <el-col :span="12" class="bg"></el-col>
    <el-col :span="6" :offset="3" class="login-form">
  <el-form size="large"
  v-if="isRegister"
        autocomplete="off"
        :model="ruleForm"
        :rules="rules">
    <el-row>
      <el-col :span="24" class="login-title">
      <h1>登录</h1>
  </el-col>
  </el-row>
  <el-form-item prop="username">
          <el-input
            v-model="ruleForm.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
            class="login-input"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="ruleForm.password"
            name="password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
            class="login-input"
          ></el-input>
        </el-form-item>
    <el-form-item >
      <el-checkbox>记住密码</el-checkbox>
    </el-form-item>
   

    <el-row>
      <el-col :span="24" class="login-title">
        <el-button type="warning" style="width: 100px;height: 40px;"  @click="login" >登录</el-button>
  </el-col>
  </el-row>
    
    <el-row class="center">
      
        <el-link :underline="false" class="one" @click="isRegister = false">注册账号</el-link>
      
        <span>|</span>
     
        <el-link :underline="false" class="twe">忘记密码</el-link>
      
      </el-row>
  
  </el-form>
  <el-form
        size="large"
        autocomplete="off"
        v-else
        :model="ruleForm"
        :rules="rules"
      >
      <el-row>
      <el-col :span="24" class="login-title">
      <h1>注册</h1>
  </el-col>
  </el-row>

        <el-form-item prop="username">
          <el-input
            :prefix-icon="User"
            placeholder="请输入用户名"
            class="login-input"
            v-model="ruleForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
          class="login-input"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
            v-model="ruleForm.password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
          class="login-input"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入再次密码"
            v-model="ruleForm.repassword"
          ></el-input>
        </el-form-item>
        <el-row>
      <el-col :span="24" class="login-title">
        <el-button type="warning" style="width: 100px;height: 40px;" @click="submit" >注册</el-button>
  </el-col>
  </el-row>
        <el-form-item class="flex">
          <el-link  :underline="false" @click="isRegister = true" style="margin-top: 20px;">
            ← 返回
          </el-link>
        </el-form-item>
      </el-form>
</el-col>
</el-row>
</template>



<style lang="scss" scoped>
.login-page {
  height: 100vh;
  background-color: #fff;
  .bg {
    background:
      url('@/assets/bgc2.jpg');
    border-radius: 20px ;
    border: 2px solid rgb(226, 162, 43);
  }
}
.login-title {
  text-align: center;
}

.el-form-item__content .el-input {
  width: 100%;
}

.flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }

.login-input{
  height: 40px;
  border: 1px solid #DCDFE6;

  border-radius: 5px;
}
.login-form{
  display: flex;
  flex-direction: column;
  justify-content:center ;
}
.center{
  text-align: center;
  display: flex;
  flex-direction: row;
  justify-content:center ;
  margin-top: 15px;
  .one{
    margin-right: 80px;
  }
  .twe{
    margin-left: 80px;
  }
}
</style>

